package com.air.service.impl;

import com.air.dao.FlightMapper;
import com.air.dao.OrderMapper;
import com.air.dao.PassengerMapper;
import com.air.entity.Order;
import com.air.entity.Passenger;
import com.air.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class PassengerServiceImpl implements PassengerService {

    @Autowired
    PassengerMapper passengerMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    FlightMapper flightMapper;

    @Override
    public Order insertPg(List<Passenger> pgs, int flightId, long id, int cang) {
        Order order = new Order();
        order.setUserId(id);
        order.setFligthId(flightId);

        //计算花费
        int cs = 0;
        int es = 0;
        for (Passenger p: pgs) {
            if (p.getAge()>12){
                cs++;
            }else{
                es++;
            }
        }

        int price=0;
        if (cang ==1){
            price=flightMapper.getFirstPriceById(flightId);
        }else if (cang == 2){
            price=flightMapper.getEconoPriceById(flightId);
        }
        int cost = price*cs + price/2*es;
        order.setCang(cang);
        order.setCost(cost);
        orderMapper.insertOrder(order);
        int orderId = (int) order.getId();
        System.out.println("订单id"+orderId);
        for (Passenger p: pgs) {
            p.setOrder(String.valueOf(orderId));
        }
        System.out.println("乘客列表"+pgs);
        int i = passengerMapper.insertPg(pgs);
        if (i <= 0){
            return null;
        }
        return order;
    }
}
